/**
 * logmanager.cpp is part of %PROJECTNAME%.
 *
 * %PROJECTNAME% is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * %PROJECTNAME% is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with %PROJECTNAME%.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "logmanager.h"
#include "logger.h"

LogManager LogManager::m_instance;

LogManager::LogManager() :
    m_loggers()
{
}

LogManager::~LogManager()
{
    tLoggers::iterator it = m_loggers.begin();
    tLoggers::iterator itEnd = m_loggers.end();
    while( it != itEnd )
    {
        delete ( it->second );
        ++it;
    }
    m_loggers.clear();
}

LogManager & LogManager::Instance()
{
    return m_instance;
}

void LogManager::SubscribeFileLogger( const std::string & a_name, log4cpp::Priority::PriorityLevel a_priority )
{
    tLoggers::iterator it = m_loggers.find( a_name );
    if( it == m_loggers.end() )
        m_loggers[ a_name ] = new Logger( a_name, FILE_LOGGER, a_priority );
}

void LogManager::SubscribeConsoleLogger( const std::string & a_name, log4cpp::Priority::PriorityLevel a_priority )
{
    tLoggers::iterator it = m_loggers.find( a_name );
    if( it == m_loggers.end() )
        m_loggers[ a_name ] = new Logger( a_name, COUT_LOGGER, a_priority );
}

const Logger * LogManager::GetLogger( const std::string & a_name ) const
{
    tLoggers::const_iterator it = m_loggers.find( a_name );
    if( it != m_loggers.end() )
        return it->second;
    return 0;
}

Logger * LogManager::GetLogger( const std::string & a_name )
{
    tLoggers::iterator it = m_loggers.find( a_name );
    if( it != m_loggers.end() )
        return m_loggers[ a_name ];
    return 0;
}
